package com.example.service.impl;

import com.example.common.PageResult;
import com.example.dto.AdminPageDto;
import com.example.entity.Admin;
import com.example.exception.CustomException;
import com.example.mapper.AdminMapper;
import com.example.service.AdminService;
import com.example.vo.AdminVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 管理员登录
     *
     * @param admin
     * @return
     */
    @Override
    public Admin login(Admin admin) {
        //1.进行非空判断
        if (admin.getUname() == null || "".equals(admin.getUname())) {
            throw new CustomException("账号不能为空");
        }
        if (admin.getUpassword() == null || "".equals(admin.getUpassword())) {
            throw new CustomException("密码不能为空");
        }
        //md5加密
        String password = DigestUtils.md5DigestAsHex(admin.getUpassword().getBytes());
        admin.setUpassword(password);
        //2.根据用户名和密码查询信息
        //账号密码都对
        //账号和密码有误，不允许登录
        Admin adminUser = adminMapper.findByNameAndPassword(admin);
        if (adminUser == null) {
            throw new CustomException("用户名或密码错误");
        }
        return adminUser;
    }

    @Override
    public PageResult findBySearch(AdminPageDto adminPageDto) {
        PageHelper.startPage(adminPageDto.getPageNum(),adminPageDto.getPageSize());
        Page<Admin> page =  adminMapper.findByNameWithUname(adminPageDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增管理员账号
     * @param admin
     */
    @Override
    public void add(Admin admin) {
        //账号和密码不能为空
        if(admin.getUname()==null||"".equals(admin.getName())){
            throw new CustomException("账号不能为空");
        }
        if(admin.getUpassword()==null||"".equals(admin.getUpassword())){
            throw new CustomException("密码不能为空");
        }
        //1.重复性判断,即账号不能一样
        Admin adminUser =  adminMapper.findByUname(admin);
        if(adminUser!=null){
            throw new CustomException("该账号已存在,请勿重复添加");
        }
        //2.初始化md5密码
        String password = DigestUtils.md5DigestAsHex(admin.getUpassword().getBytes());
        admin.setUpassword(password);
        adminMapper.add(admin);
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    @Override
    public void edit(Admin admin) {
        Admin ad= admin;
        //如果传进来密码为空，则查询该id的管理员密码重新赋值
        if(ad.getUpassword()==null){

            Admin adminUser =  adminMapper.getById(admin.getId());
            ad.setUpassword(adminUser.getUpassword());
        }
        String password = DigestUtils.md5DigestAsHex(ad.getUpassword().getBytes());
        ad.setUpassword(password);
        adminMapper.update(ad);
    }
}
